package com.util.vmwareops;

import com.vmware.vim25.mo.ClusterComputeResource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class ClusterOps {
    // 将集群信息更新至数据库
    public synchronized static List<ClusterInfo> updateCluster(Connection con, VCenterInfo vc, List<ClusterComputeResource> clusterList, String versionId) throws SQLException {
        System.out.println(String.format("[INFO] Update Cluster start"));

        List<ClusterInfo> clusterInfo = new ArrayList<ClusterInfo>();

        Statement stmt = null;
        stmt = con.createStatement();

        for(ClusterComputeResource cluster : clusterList) {
            ClusterInfo tmp = new ClusterInfo(
                    MyUtils.getUUID("CLUSTER"),
                    vc,
                    cluster.getMOR().getVal(),
                    cluster.getName(),
                    cluster.getSummary().getNumCpuCores(),
                    cluster.getSummary().getTotalMemory() / 1024.0 / 1024.0 / 1024.0,
                    versionId
            );
            clusterInfo.add(tmp);
            String sql_command = String.format(
                    "insert into resource_bundle_vsphere_cluster values(\"%s\", \"%s\", \"%s\", \"%s\", %d, %.2f, \"%s\")",
                    tmp.getId(),
                    tmp.getVc().getId(),
                    tmp.getDomainName(),
                    tmp.getName(),
                    tmp.getCpu(),
                    tmp.getMem(),
                    tmp.getVersionId()
            );
            // System.out.println(sql_command);
            stmt.execute(sql_command);
        }

        System.out.println(String.format("[INFO] Update Cluster end"));
        return clusterInfo;
    }
    public static Map<String, ClusterInfo> getClusterInfoByVersionId(String versionId, Map<String, VCenterInfo> vCenterInfoMap) throws InterruptedException {
        DBConnectionPool db = DBConnectionPoolFactory.getDBConnectionPool("vmwareops");
        Connection con = db.getConnection();
        Map<String, ClusterInfo> clusterInfos = new HashMap<String, ClusterInfo>();
        String sql = String.format("select * from %s where version=\'%s\'", "resource_bundle_vsphere_cluster", versionId);
        try {
            Statement stmt = con.createStatement();
            ResultSet res = stmt.executeQuery(sql);
            if(res == null) {
                return null;
            }
            while(res.next()) {
                clusterInfos.put(
                        res.getString("id"),
                        new ClusterInfo(
                                res.getString("id"),
                                vCenterInfoMap.get(res.getString("vc_id")),
                                res.getString("cluster_domain_name"),
                                res.getString("cluster_name"),
                                res.getInt("cpu"),
                                res.getDouble("memory"),
                                res.getString("version")
                        )
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(con != null) {
                db.close(con);
            }
        }
        return clusterInfos;
    }
}
